package demo01;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Random;


public class zuoye06 {
	public static void main(String[] args) throws Exception {
		int onlyNum=getOnlyNumber();
		System.out.println(onlyNum);
			
	BufferedWriter wr=new BufferedWriter(new FileWriter("luckNumber.txt",true));
	wr.write(onlyNum+"\n");
	wr.close();

	BufferedReader rd=new BufferedReader(new FileReader("onlyGetone.txt"));
	String line=null;
	int lineCount=1;
	while((line=rd.readLine())!=null)
	{
		if(onlyNum==lineCount)
		{
			System.out.println(line);
	}
		lineCount++;
	}
		BufferedReader rr=new BufferedReader(new FileReader("luckNumber.txt"));
		int len=1;
		String line0=null;
		while((line0=rr.readLine())!=null)
		{
			if (len == 10) {
			    BufferedWriter clear = new BufferedWriter(new FileWriter("luckNumber.txt"));
			    clear.write("0"); 
			    clear.close(); 
			}
			len++;
		}
	}
	private static int getOnlyNumber() throws Exception {
		// TODO Auto-generated method stub
		Random rn=new Random();
		int luckNum=rn.nextInt(10)+1;
		while (true) {
            luckNum = rn.nextInt(10) + 1;

            if (!isDuplicate(luckNum)) {
            	break; 
            }
        }
		return luckNum;
	}

	private static boolean isDuplicate(int luckNum) throws Exception, IOException {
		// TODO Auto-generated method stub
		BufferedReader rr=new BufferedReader(new FileReader("luckNumber.txt"));
		String line=null;
		boolean only=false;
		while((line=rr.readLine())!=null)
		{
			int num = Integer.parseInt(line.trim());
			if(luckNum==num)
			{
				only=true;
				break;
			}
		}
		rr.close();
		return only;
	}

}